# Function to decode and verify a JWT
from fastapi import HTTPException
from jose import jwt, JWTError
from starlette.websockets import WebSocket


# Secret key and algorithm for JWT
SECRET_KEY = "your_secret_key"
ALGORITHM = "HS256"


def decode_jwt(token: str):
    try:
        return jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
    except JWTError as e:
        raise HTTPException(status_code=403, detail=f"Invalid token: {str(e)}")

def verify_token(token: str):
    try:
        return decode_jwt(token)
    except JWTError as e:
        raise HTTPException(status_code=403, detail=f"Invalid token: {str(e)}")


# Function to create a JWT
def create_jwt(data: dict):
    from datetime import datetime, timedelta

    to_encode = data.copy()
    expire = datetime.utcnow() + timedelta(hours=1)
    to_encode.update({"exp": expire})
    return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)

